from typing import Optional
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def build_list(elements):
    dummy = ListNode()
    p = dummy
    for element in elements:
        p.next = ListNode(element)
        p = p.next
    return dummy.next